package com.yhos.platform.oopston.core.aop;

import com.yhos.platform.oopston.core.annotation.ParamsValid;
import com.yhos.platform.oopston.core.exception.BusinessException;
import com.yhos.platform.oopston.core.manager.ValidManager;
import com.yhos.platform.oopston.core.utils.MessageSourceUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

public class ValidatorAspect {

    private static Logger logger = LoggerFactory.getLogger(ValidatorAspect.class);
    private static ValidManager validManager = ValidManager.getInstance();

    private MessageSourceUtils messageSourceUtils;

    public void controllerMethodPointcut() {

    }

    public Object processAspect(ProceedingJoinPoint point) throws Throwable {
        Object[] args = point.getArgs();
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();

        checkRestValid(method, args);

        return point.proceed();
    }

    private void checkRestValid(Method method, Object[] args) throws BusinessException {
        Annotation[][] parameterAnnotations = method.getParameterAnnotations();
        if (parameterAnnotations != null && parameterAnnotations.length > 0) {
            //多个参数循环
            for (int i = 0; i < parameterAnnotations.length; i++) {
                Annotation[] paramAnn = parameterAnnotations[i];
                if (paramAnn != null && paramAnn.length > 0) {
                    //参数有多个注解
                    for (int j = 0; j < paramAnn.length; j++) {
                        Annotation annotation = paramAnn[j];
                        if (ParamsValid.class.isInstance(annotation)) {
                            validManager.valid(args[i], messageSourceUtils);
                            break;
                        }
                    }
                }
            }
        }
    }

    public void setMessageSourceUtils(MessageSourceUtils messageSourceUtils) {
        this.messageSourceUtils = messageSourceUtils;
    }
}
